<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"  xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" >
<head th:include="/include/commonHeader::commonHeader('值班安排新增')"></head>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form class="form-horizontal m-t" id="exampleForm">
                        <input id="moduleFlag" name="moduleFlag"  type="hidden" th:value="${moduleFlag}">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">日期：</label>
                            <div class="col-sm-8">
                                <input id="flightDate" name="flightDate" class="form-control" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">值班类型：</label>
                            <div class="col-sm-8">
                                <select id="dutyCode" name="dutyCode" class="form-control" ></select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">值班人：</label>
                            <div class="col-sm-8">
                                <select id="pCode1" name="pCode1" class="form-control"></select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="text-center">
                                <button id="btn_save" onclick="save()" type="button" class="btn btn-primary">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
	</div>

    <script src="/plugins/bootstrapValidator/js/bootstrapValidator.min.js"></script>
    <script src="/plugins/bootstrapValidator/js/language/zh_CN.js"></script>

	<script th:inline="javascript">
        var prefix = "/dutySchedule";
        var moduleFlag = [[${moduleFlag}]];
        $(function(){
            initValidator();

            var paramArr = [{'moduleFlag':moduleFlag}];
            select2CommonAsync("#pCode1","/select2/getStaffPCode.do","人员姓名/首字母",null,paramArr);
            select2InitForData("#dutyCode","/code/getCode.do?parentCode=DUTY_TYPE&moduleFlag="+moduleFlag,null,null,false,null);

            //初始化日期控件
            $("#flightDate").datetimepicker({
                minView: "month",
                language: 'zh-CN',
                format: 'yyyy-mm-dd',
                autoclose: true,
                todayBtn: true,
                //startDate: new Date()
            }).on('changeDate', function (ev) {
                $("#exampleForm").data('bootstrapValidator')
                    .updateStatus('flightDate', 'NOT_VALIDATED')
                    .validateField('flightDate')
                    .updateStatus('dutyCode', 'NOT_VALIDATED')
                    .validateField('dutyCode');
            });

            $("#dutyCode").change(function(){
                $("#exampleForm").data('bootstrapValidator')
                    .updateStatus('flightDate', 'NOT_VALIDATED')
                    .validateField('flightDate')
                    .updateStatus('dutyCode', 'NOT_VALIDATED')
                    .validateField('dutyCode');
            });

        });


        //保存
        function save(){
            $("#exampleForm").data('bootstrapValidator').validate();//触发一次所有的验证
            if(!$("#exampleForm").data('bootstrapValidator').isValid()){//判断验证是否已经通过
                return;
            }

            $.ajax({
                cache : true,
                type : "POST",
                url : prefix + "/save.do",
                data : $('#exampleForm').serialize(),
                async : false,
                error : function(request) {
                    parent.layer.alert("Connection error");
                },
                success : function(data) {
                    if (data.code == 0) {
                        parent.layer.msg("操作成功");
                        parent.reload();
                        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                        parent.layer.close(index);
                    } else {
                        parent.layer.alert(data.msg)
                    }
                }
            });
        }

        //初始化验证规则
        function initValidator(){
           $('#exampleForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                flightDate: {
                    message: 'The value is not valid',
                    validators: {
                        notEmpty: {
                            message: '日期不能为空'
                        },
                        remote: {
                            delay: 1,
                            message: '同一天同一类型不能重复',
                            url: prefix+'/check.do',
                            data: function (validator) {
                                return {
                                    dutyCode: $("#dutyCode").val()
                                };
                            }
                        }
                    }
                },
                dutyCode: {
                    message: 'The value is not valid',
                    validators: {
                        notEmpty: {
                            message: '备份类型不能为空'
                        },
                        remote: {
                            delay: 1,
                            message: '同一天同一类型不能重复',
                            url: prefix+'/check.do',
                            data: function (validator) {
                                return {
                                    flightDate: $("#flightDate").val()
                                };
                            }
                        }
                    }
                }
            }
        });
        }
	</script>
</body>
</html>
